package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCLogin2 {
    public static void main(String[] args) {
        try {
            Connection connection = DBUtil.getConnection();

            Scanner sc = new Scanner(System.in);
            System.out.println("请输入账号: ");
            String username = sc.nextLine();
            System.out.println("请输入密码: ");
            String password = sc.nextLine();
            //定义预编译SQL语句[?代表占位符]
            String sql = "select nickname from userinfo where username=? and password=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            //设置?的值
            ps.setString(1, username);
            ps.setString(2, password);
            //执行预编译SQL
            ResultSet r = ps.executeQuery();
            if (r.next()){
                System.out.println("登录成功,欢迎回来"+r.getString("nickname"));
            }else {
                System.out.println("用户名或密码错误");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
